package com.shadow.baiqun.delegate;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.config.ResponseCode;
import com.shadow.baiqun.config.SpKey;
import com.shadow.core.delegate.RxShadowDelegate;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;
import com.shadow.core.util.md5.MD5;
import com.shadow.core.util.storage.ShadowPreference;
import com.shadow.core.util.toast.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Create by zy
 * on 2018/5/19
 *
 * @author Administrator
 */
public class LoginDelegate extends RxShadowDelegate {
    @BindView(R2.id.et_number)
    EditText mEtNumber;
    @BindView(R2.id.et_password)
    EditText mEtPassword;

    @Override
    public Object setLayout() {
        return R.layout.delegate_login;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        autoLogin();
    }

    private void autoLogin() {
        String token = ShadowPreference.getCustomAppProfile(ResponseCode.TOKEN);
        if (!token.isEmpty()) {
            RxRestClient.builder()
                    .url("WMS/system_login")
                    .loader(getContext())
                    .params("KEYDATA", "2,df," + token)
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            try {
                                final JSONObject jsonObject = JSON.parseObject(response);
                                String state = jsonObject.getString("result");
                                if (ResponseCode.SUCCESS.equals(state)) {
                                    start(new HomeDelegate());
                                }
                            } catch (Exception e) {
                                ToastUtils.showToast(getContext(), "服务器返回数据有误!");
                                e.printStackTrace();
                            }
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {
                            ToastUtils.showToast(getContext(), msg);
                        }
                    })
                    .build()
                    .unRestPost();
        }
    }

    @OnClick(R2.id.btn_login)
    public void onLogin() {
        if (isFastClick()) {
            return;
        }
        final String number = mEtNumber.getText().toString();
        String password = mEtPassword.getText().toString();
        if (number.isEmpty() || password.isEmpty()) {
            ToastUtils.showToast(getContext(), "请输入正确的用户名和密码!");
            return;
        }
        ShadowPreference.addCustomAppProfile("Cookie", "");
        RxRestClient.builder()
                .url("WMS/system_login")
                .loader(getContext())
                .params("KEYDATA", "2,df,0,df," + number + ",df," + MD5.stringToMD5(password))
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        try {
                            final JSONObject jsonObject = JSON.parseObject(response);
                            String state = jsonObject.getString("result");
                            if (ResponseCode.SUCCESS.equals(state)) {
                                start(new HomeDelegate());
                                String token = jsonObject.getString("token");
                                ShadowPreference.addCustomAppProfile(SpKey.KEY_USER_ACCOUNT, number);
                                if (token != null && !token.isEmpty()) {
                                    ShadowPreference.addCustomAppProfile(ResponseCode.TOKEN, token);
                                }
                            }
                        } catch (Exception e) {
                            ToastUtils.showToast(getContext(), "服务器返回数据有误!");
                            e.printStackTrace();
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        ToastUtils.showToast(getContext(), msg);
                    }
                })
                .build()
                .unRestGet();
    }
}
